using System;
using System.Data;
using NHibernate;
using Project.Library.Core.DBC;

namespace Project.Library.NHibernate
{
    /// <summary>
    /// Manage a transaction for a given session
    /// </summary>
    public class TransactionManager : ITransactionManager
    {
        private ISession currentSession;

        /// <summary>
        /// Sets the session.
        /// </summary>
        /// <param name="session">The session.</param>
        public void SetSession(ISession session)
        {
            currentSession = session;
        }

        /// <summary>
        /// Creates a new transaction with the default isolation level: ReadCommited.
        /// </summary>
        /// <returns></returns>
        public ITransaction CreateTransaction()
        {
            return CreateTransaction(IsolationLevel.ReadCommitted);
        }

        /// <summary>
        /// Creates a new transaction with the specified isolation level.
        /// </summary>
        /// <param name="isolationLevel">The isolation level.</param>
        /// <returns></returns>
        public ITransaction CreateTransaction(IsolationLevel isolationLevel)
        {
            Checks.Require(currentSession.Transaction.IsActive, "You are already in a transaction");
            return currentSession.BeginTransaction(isolationLevel);
        }

        /// <summary>
        /// Commits the pendent operations in session.
        /// </summary>
        public void Commit()
        {
            Checks.Require(!currentSession.Transaction.IsActive, "There is no transaction started. Call 'CreateTransaction' first");
            currentSession.Transaction.Commit();
        }

        /// <summary>
        /// Rolls the back.
        /// </summary>
        public void RollBack()
        {
            Checks.Require(!currentSession.Transaction.IsActive, "There is no transaction started. Call 'CreateTransaction' first");
            currentSession.Transaction.Rollback();
        }

        /// <summary>
        /// Transactionals the flush.
        /// </summary>
        public void TransactionalFlush()
        {
            TransactionalFlush(IsolationLevel.ReadCommitted);
        }

        /// <summary>
        /// Transactionals the flush.
        /// </summary>
        /// <param name="isolationLevel">The isolation level.</param>
        public void TransactionalFlush(IsolationLevel isolationLevel)
        {
            Checks.Require(currentSession.Transaction.IsActive, "You are already in a transaction");
            
            var transaction = currentSession.BeginTransaction(isolationLevel);
            try
            {
                transaction.Commit();
            }
            catch (Exception)
            {
                transaction.Rollback();
                throw;
            }
            finally
            {
                transaction.Dispose();
            }
        }
    }
}